package com.kj.tms.system.controller.basicinfo;


import com.kj.tms.common.bean.dto.Pager;
import com.kj.tms.common.bean.dto.ReturnData;
import com.kj.tms.common.utils.constant.Consts;
import com.kj.tms.common.utils.uuid.IdGen;
import com.kj.tms.pri.system.basicinfo.dto.BaseMessageDto;
import com.kj.tms.pri.system.basicinfo.dto.ShareholderDto;
import com.kj.tms.pri.system.basicinfo.service.BaseMessageService;
import com.kj.tms.pri.system.basicinfo.service.TaxPayersInfoService;
import com.kj.tms.pri.system.menu.dto.DictDto;
import com.kj.tms.pri.system.menu.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: tms
 * @Package: com.hongtu.tms.taxdeclare.controller
 * @ClassName: ShareholderController
 * @Description: 股东信息
 * @Author: [yangruidong]
 * @CreateDate: 2018/3/20 17:18
 * @UpdateUser: [修改者姓名]
 * @UpdateDate: 2018/3/20 17:18
 * @UpdateRemark: [修改原因备注]
 * @Version: 1.0
 * Copyright: 北京宏图天安 Copyright (c) ${YEAR}
 */
@Controller
@RequestMapping("/system/shareholder")
public class ShareholderController {
    @Autowired
    private BaseMessageService baseMessageService;
    @Autowired
    private DictService dictMapper;
    @Autowired
    private TaxPayersInfoService taxIdentifyMapper;

    /**
     * @Method [query]
     * @Description [加载基础信息]
     * @Author [yangruidong]
     * @Version 1.0
     * @Param matterId 事项ID  taxpayersInfoId 纳税主体
     * @Return
     * @Exception
     * @Date
     */
    @RequestMapping("/query")
    @ResponseBody
    public ReturnData query(@RequestParam(value = "matterId", required = false) String matterId,
                            @RequestParam(value = "taxpayersInfoId", required = false) String taxpayersInfoId) {
        BaseMessageDto dto = new BaseMessageDto();
        dto = baseMessageService.query(matterId, taxpayersInfoId);
        return new ReturnData(Consts.SUCCESS, "", dto);
    }

    /**
     * @Method [getPage]
     * @Description [加载股东信息]
     * @Author [yangruidong]
     * @Version 1.0
     * @Param id 主键
     * @Return
     * @Exception
     * @Date
     */
    @RequestMapping("/getPage")
    @ResponseBody
    public ReturnData getPage(@RequestParam(value = "id", required = false) String id) {

        Map<String, Object> returnObject = new HashMap<String, Object>();
        //修改显示
        if (!"".equals(id) && id != null) {
            ShareholderDto shareholderDto = new ShareholderDto();
            shareholderDto.setTaxpayersinfo(id);
            Pager pager = baseMessageService.listByPager(shareholderDto);
            returnObject.put("total", pager.getRowCount());
            returnObject.put("rows", pager.getResults());
        }

        return new ReturnData(Consts.SUCCESS, "", returnObject);
    }

    /**
     * @Method [Asave]
     * @Description [基础信息保存或更新]
     * @Author [yangruidong]
     * @Version 1.0
     * @Param dto
     * @Return
     * @Exception
     * @Date
     */
    @RequestMapping("/Asave")
    @ResponseBody
    public ReturnData baseMessageSave(BaseMessageDto dto) {

        if ("".equals(dto.getBaseId()) || dto.getBaseId() == null) {
            dto.setBaseId(IdGen.getId());
            baseMessageService.save(dto);
        } else {
            baseMessageService.update(dto);
        }
        return new ReturnData(Consts.SUCCESS, "保存成功", "");
    }

    /**
     * @Method [toAdd]
     * @Description [跳转增加股东页面]
     * @Author [yangruidong]
     * @Version 1.0
     * @Param model
     * @Return
     * @Exception
     * @Date
     */

    @RequestMapping("/toAdd")
    public String toAdd(Model model) {
        DictDto dto = new DictDto();
        dto.setDictType("ZJLX");
        List<DictDto> list = dictMapper.listByDictDto(dto);
        model.addAttribute("list", list);
        return "/basicinfo/taxPayersInfo/add_shareholder";
    }

    /**
     * @Method toUpdate
     * @Param id
     * @Return
     * @Exception 跳转增加股东页面
     * @Description [方法描述]
     * @Author yangruidong
     * @Version 1.0
     * @Date 2018/3/28 11:02
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(Model model, @RequestParam(value = "id", required = false) String id) {
        DictDto dto = new DictDto();
        dto.setDictType("ZJLX");
        List<DictDto> list = dictMapper.listByDictDto(dto);
        model.addAttribute("list", list);
        model.addAttribute("id", id);

        ShareholderDto shareholderDto = new ShareholderDto();
        shareholderDto.setId(id);
        List<ShareholderDto> shareholderList = baseMessageService.listByCondition(shareholderDto);
        model.addAttribute("shareholderList", shareholderList.get(0));
        return "/basicinfo/taxPayersInfo/update_shareholder";
    }

    /**
     * @Method update
     * @Param dto 股东信息dto
     * @Return 更新成功返回信洗
     * @Exception
     * @Description [方法描述]
     * @Author yangruidong
     * @Version 1.0
     * @Date 2018/3/28 11:26
     */
    @RequestMapping("/update")
    @ResponseBody
    public ReturnData update(ShareholderDto dto) {
        baseMessageService.updateShareholder(dto);
        //baseMessageMapper.updateA0000002(dto);
        return new ReturnData(Consts.SUCCESS, "更新成功", "");
    }

    /**
     * @Method [saveShareholder]
     * @Description [保存股东信息]
     * @Author [yangruidong]
     * @Version 1.0
     * @Param dto
     * @Return
     * @Exception
     * @Date
     */
    @RequestMapping("/saveShareholder")
    @ResponseBody
    public ReturnData saveShareholder(ShareholderDto dto) {
        dto.setId(IdGen.getId());
        List<ShareholderDto> list = new ArrayList<ShareholderDto>();
        list.add(dto);
        taxIdentifyMapper.batchInsertShareholder(list);
        return new ReturnData(Consts.SUCCESS, "更新成功", "");
    }

}
